<?php
if ((isset($_SESSION['identifiant'])) AND (isset($_SESSION['groupe'])==2))
{
	$types_mondes = array ('Base','Dimension', 'Galaxie', 'Système solaire', 'Planète', 'Lune/satellite', 'Continent', 'Pays/royaume', 'Lieu naturel', 'Construction extérieure', 'Ville',  'Batiment', 'Piece');
	$nombre_type = count($types_mondes); 

	if (isset($_POST['ajouter'])) 
	{
		$req = $bdd->prepare('INSERT INTO monde (id_parent, type_parent, nom, type_monde) VALUES (:id_parent, :type_parent, :nom, :type_monde)');
		$req->execute(array(
			'id_parent' => $_POST['id_parent'],
			'type_parent' => $_POST['id_parent'],
			'nom' => $_POST['nom_monde'],
			'type_monde' => $_POST['type_monde']
		));
		header('Location: page.php?page=admin_mondes'); 
	}
	elseif (isset($_POST['editer'])) 
	{
		$req = $bdd->prepare('SELECT * FROM monde WHERE id = :id');
		$req->execute(array('id' => $_POST['id']));
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="page.php?page=admin_mondes">
				<table>
				<tr><td><label for="nom_monde">Nom</label></td>
				<td><input type="text" name="nom_monde" id="nom_monde" value ="<?php echo $donnees['nom']; ?>" size="20" maxlength="100" /></td></tr>
				<tr><td><label for="type_monde">Type de monde</label></td>
				<td><select name="type_monde" id="type_monde">
					<?php
					$i=0;
					foreach($types_mondes as $type) {
						if ($i > 0) { 
							if ($i == $donnees['id']) {
								?><option value="<?php echo $i; ?>" selected="selected"><?php echo '('.$i.')'.$types_mondes[$i]; ?></option><?php
							}
							?>
							<option value="<?php echo $i; ?>"><?php echo '('.$i.')'.$types_mondes[$i]; ?></option>
							<?php 
						}
						$i++;
					}
					?>
				</select></td></tr>
				<tr>
				<td><label for="id_parent">Monde parent</label></td>
				<td><select name="id_parent" id="id_parent">
				<?php
					$req_id = $bdd->prepare('SELECT * FROM monde');
					$req_id->execute(array('id_parent' => $donnees['id_parent']));
					
					while ($donnee_id = $req_id->fetch())
					{
						if ($donnee_id['id'] == $donnees['id_parent'])
						{
							?><option value="<?php echo $donnee_id['id']; ?>" selected="selected"><?php echo $donnee_id['nom']; ?></option><?php
						}
						else {
							?><option value="<?php echo $donnee_id['id']; ?>"><?php echo $donnee_id['nom']; ?></option><?php
						}
					}
					$req_id->closeCursor();
				?>
				</select></td></tr>
				<tr><td colspan="2"><label for="description">Description</label></td></tr>
				<tr><td colspan="2"><TEXTAREA name="description" rows="3" cols="60"><?php echo $donnees['description']; ?></TEXTAREA></td></tr>
				<tr><td><input type="submit" name="modifier" value="Modifier" /></td><td></td></tr>
				</table>
			</form>
			<?php
		}
		$req->closeCursor();
	}
	elseif (isset($_POST['modifier'])) 
	{
		$req = $bdd->prepare('UPDATE monde SET id_parent = :id_parent, type_parent = :type_parent, nom = :nom, type_monde = :type_monde, description = :description WHERE id=:id');
		$req->execute(array(
			'id_parent' => $_POST['id_parent'],
			'type_parent' => $types_mondes[$i],
			'nom' => $_POST['nom_monde'],
			'type_monde' => $_POST['type_monde'],
			'description' => $_POST['description']
		));
		
		
	}
	else
	{
		?>
		<h1 id="titre">Gestion des mondes</h1>
		<table width="100%">
			<tr><th>TYPE</th><th>PARENT (nb enfants)</th><th>NOM</th><th> </th></tr>
			<tr><td colspan="4"><hr /></td></tr>
			<tr><td><em>Non modifiable</em></td><td>   -</td><td><em>Base du monde</em></td><td></td></tr>
			<form method="post" action="page.php?page=admin_mondes">
				<?php
					$req = $bdd->query('SELECT * FROM monde WHERE id > 0 ORDER BY type_monde, nom ASC');
					while ($donnees = $req->fetch())
					{
						// recherche du nom du parent selon l'id parent
						$req_id = $bdd->prepare('SELECT * FROM monde WHERE id = :type_monde');
						$req_id->execute(array('type_monde' => $donnees['type_monde']));
						$donnee_id = $req_id->fetch();
						$position = $donnees['type_monde'];
						?>
						<tr>
						<td><?php echo $types_mondes[$position]; ?></td>
						<td>
						<?php
						$req_id = $bdd->prepare('SELECT nom FROM monde WHERE id = :id_parent');
						$req_id->execute(array('id_parent' => $donnees['id_parent']));
						$donnee_id = $req_id->fetch();
						echo $donnee_id['nom'].'('.$donnees['enfants'].')'; 
						?>
						</td>
						<td><?php echo $donnees['nom']; ?></td>
						<td><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
						    <input type="submit" name="editer" value="Editer" /></td>
						</tr>
						<?php
					}
					$req->closeCursor();
				?>
				<tr><td colspan="4"><hr /></td></tr>
				<tr>
				<td><select name="type_monde" id="type_monde">
					<?php
					foreach($types_mondes as $i => $types_mondes)
					{
						if ($i > 0) { 
							?>
							<option value="<?php echo $i; ?>"><?php echo '('.$i.')'.$types_mondes; ?></option>
							<?php 
						}
					}
					?>
				</select>
				</td>
				
				<td>
				<SELECT name="id_parent">
					<?php
					$req_id = $bdd->query('SELECT * FROM monde ORDER BY type_monde ASC');
					while ($donnee_id = $req_id->fetch())
					{
						?><option value="<?php echo $donnee_id['id']; ?>"><?php echo $donnee_id['nom']; ?></option><?php
					}
					$req_id->closeCursor();
					?>
				</SELECT>
				</td>
				
				<td><input type="text" name="nom_monde" id="nom_monde" size="20" maxlength="100" /></td>
				<td><input type="submit" name="ajouter" value="Ajouter" /></td>
				</tr>
			</form>
		</table>
		<?php
	}
}
else
{
	echo NON_ACCES_ADMIN;
}
?>